﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.IO;

namespace AutoDataUpdateTool
{
    public class DataSender
    {
        private string SqlLineTxt;
        private List<int> RouteIdList;

        public string SqlLineRes {
            get { return SqlLineTxt; }
        }
        public DataSender(List<BusRouteInfo> busRouteInfoList,string routeid) {
     
            Initializer();
            ModifyTblrouted(busRouteInfoList, routeid);                    
        }
        public void Initializer() {
            SqlLineTxt = "";
            RouteIdList = new List<int>();
        }
   
        /// <summary>
        /// fetch the content in the list to sql server
        /// </summary>
        /// <param name="busRouteInfoList"></param>
        /// <param name="routeid"></param>
        public void ModifyTblrouted(List<BusRouteInfo> busRouteInfoList,string routeid) 
        {
            //DataSet ds = DbHelperSQL.Query("select 线路内序号,线路ID from tblrouted");
            if (busRouteInfoList.Count <= 0) { SqlLineTxt = "no content available to insert or update!"; return; }
            string exsqlLine = "UPDATE tblRouteD SET 线路ID='" + routeid + "_TEMP' WHERE 线路ID='" + routeid + "';";
            try
            {
                DbHelperSQL.ExecuteSql(exsqlLine);
                SqlLineTxt += DateTime.Now.ToString("yy-MM-dd HH:mm:ss") + " #{ " + exsqlLine + " }# " + Environment.NewLine;
            }
            catch (Exception e) { }  
            foreach (BusRouteInfo bri in busRouteInfoList) {
                
                string crm = CarRuninMode(bri.stationType , bri.direction);
                if (crm == "Unknown")
                {
                    exsqlLine = "INSERT INTO tblRouteD (线路ID,距离,线路内序号,站码GUID,方向,站名,经度,纬度,经度60,纬度60) VALUES ('"
                        + routeid + "',500,'" + bri.stationSequence + "','" + bri.stationSequence + "','" + bri.direction + "','" + bri.stationName + "','"
                        + bri.latitude + "','" + bri.longitude + "','" + bri.latitude + "','" + bri.longitude + "');";
                }
                else
                {
                    exsqlLine = "INSERT INTO tblRouteD (线路ID,距离,线路内序号,站码GUID,方向,站名,经度,纬度,经度60,纬度60," + crm + ") VALUES ('"
                        + routeid + "',500,'" + bri.stationSequence + "','" + bri.stationSequence + "','" + bri.direction + "','" + bri.stationName + "','"
                        + bri.latitude + "','" + bri.longitude + "','" + bri.latitude + "','" + bri.longitude + "',1);";
                 }
                try
                {
                    DbHelperSQL.ExecuteSql(exsqlLine);
                    SqlLineTxt += DateTime.Now.ToString("yy-MM-dd HH:mm:ss") + " #{ " + exsqlLine + " }# " + Environment.NewLine;
                }
                catch (Exception e) { }         
            }
           
            string filename = "SQLline.log";
            using (FileStream fsWrite = new FileStream(filename, FileMode.Append))
            {
                 byte[] myByte = System.Text.Encoding.UTF8.GetBytes(SqlLineTxt);
                 fsWrite.Write(myByte, 0, myByte.Length);
            };
        }

        public string CarRuninMode(string type,string direction) {
            if (direction  == "上行" && type == "起点站") { return "上行始发"; }
            else if (direction  == "上行" && type == "终点站") { return "上行终点"; }
            else if (direction  == "下行" && type == "起点站") { return "下行始发"; }
            else if (direction == "下行" && type == "终点站") { return "下行终点"; }
            else { return "Unknown"; }
        }
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                string _connectionString = ConfigurationManager.AppSettings["ConnectionString"];
                string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
                //if (ConStringEncrypt == "true")
                //{
                //    _connectionString = DESEncrypt.Decrypt(_connectionString);
                //}
                return _connectionString;
            }
        }

        /// <summary>
        /// 得到web.config里配置项的数据库连接字符串。
        /// </summary>
        /// <param name="configName"></param>
        /// <returns></returns>
        public static string GetConnectionString(string configName)
        {
            string connectionString = ConfigurationManager.AppSettings[configName];
            string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
            //if (ConStringEncrypt == "true")
            //{
            //    connectionString = DESEncrypt.Decrypt(connectionString);
            //}
            return connectionString;
        }
    }
}
